
//前端的用户业务逻辑接口,这里封装了用户的所有操作

//1.用户登录

function login(identify,password,code,token){
    const promise = new Promise((resolve, reject) => {
        $.ajax({
            url: baseUrl + 'users/auth?identify='+identify+'&password='+password+"&code="+code+"&token="+token,
            type: 'GET',
            dataType: 'json',
            success: resp => {
                resolve(resp);

            },
            error: err => {
                reject(err);
            }
        })
    })
    return promise;
}

//2.用户注册
async function reg(user) {
    const promise = new Promise((resolve, reject) => {
        $.ajax({
            url: baseUrl + 'users/',
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json',
            data: JSON.stringify(user), //json对象转换为字符串。
            success: resp => {
                resolve(resp);
            },
            error: err => {
                reject(err);
            }
        })
    })
    return promise;
}


//给手机发送验证码的方法
function sendRandomCodeMessage(token,identify){

    const promise = new Promise((resolve, reject) => {
        $.ajax({
            url: 'https://www.simoniu.com/commons/msg/validatecode?identify='+identify+'&token='+token,
            type: 'GET',
            dataType: 'json',
            success: resp => {
                resolve(resp);
            },
            error: err => {
                reject(err);
            }
        })
    })
    return promise;
}

async function validateRandomCode(token,code){
    const promise = new Promise((resolve, reject) => {
        $.ajax({
            url: 'https://www.simoniu.com:18991/examdemo/commons/msg/auth?code='+code+'&token='+token,
            type: 'GET',
            dataType: 'json',
            success: resp => {
                resolve(resp);
            },
            error: err => {
                reject(err);
            }
        })
    })
    return promise;
}

//检查手机号码是否已经注册

async function checkMobileIsReg(mobile){
    const promise = new Promise((resolve, reject) => {
        $.ajax({
            url: baseUrl+'users/isReg?mobile='+mobile,
            type: 'GET',
            dataType: 'json',
            success: resp => {
                resolve(resp);
            },
            error: err => {
                reject(err);
            }
        })
    })
    return promise;
}


